package cn.tom.controller;

import cn.tom.entity.*;
import cn.tom.respjson.RespJson;
import cn.tom.service.*;
import cn.tom.tool.ImageBinaryUtil;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Map;

@Controller //实例化装配到IOC容器
@RequestMapping("/api/adm")   //管理员路径
public class AdmController {
    @Autowired
    InformationService informationService;  //注入InformationService接口
    @Autowired
    InformationTypeService informationTypeService; //注入InformationTypeService接口
    @Autowired
    TeacherService teacherService;                  //注入TeacherService接口
    @Autowired
    MyUserService myUserService;                    //注入MyUserService接口
    @Autowired
    CertificateService certificateService;          //注入CertificateService接口
    @Autowired
    ProductIpfsService productIpfsService;          //注入ProductIpfsService接口
    @Autowired
    GroupService groupService;                      //注入GroupService接口
    @Autowired
    LargefileService largefileService;              //注入LargeFileService接口
    @Autowired
    DevelopmentHistoryService developmentHistoryService;  //注入发展历程的接口
    @Autowired
    ElegantDemeanourService elegantDemeanourService;   //注入我们的风采模块接口
    @Autowired
    HeatMapService heatMapService;       //注入热图接口
    @Autowired
    RecruitmentService recruitmentService;    //注入诚聘英才接口
    @Autowired
    PublicInterestService publicInterestService;  //导入公益活动接口

    //Logger输出
    private Logger log
            = LoggerFactory.getLogger(cn.tom.tool.Log.class);


    /**
     * 添加一个资讯
     *
     * @param information
     * @param mpfile
     * @return
     * @throws IOException
     */
    @PostMapping("/information/add")
    @ResponseBody
    //@RequestParam 文件上传
    public Map<String, Object> add(Information information, @RequestParam(value = "pic", required = false) MultipartFile mpfile) throws IOException {
        if (mpfile != null) {   //文件不为空执行
            File fileTp = new File("/static");
            FileUtils.copyInputStreamToFile(mpfile.getInputStream(), fileTp);   //转成二进制
            information.setPicture(ImageBinaryUtil.getImageBinary(fileTp));    //赋值
        }
        int add = informationService.add(information);  //执行添加
        if (add == 1) {
            return RespJson.pack(200, "添加成功", null);
        } else {
            return RespJson.pack(500, "添加失败", null);
        }
    }


    /**
     * 更新一个资讯
     *
     * @param information
     * @param mpFile
     * @return
     * @throws IOException
     */
    @PostMapping("/information/update")
    @ResponseBody
    //@RequestParam 文件上传
    public Map<String, Object> update(Information information, @RequestParam(value = "pic", required = false) MultipartFile mpFile) throws IOException {
        if (mpFile != null) {   //文件不为空执行
            File fileTp = new File("/static");
            FileUtils.copyInputStreamToFile(mpFile.getInputStream(), fileTp);   //转成二进制
            information.setPicture(ImageBinaryUtil.getImageBinary(fileTp));    //赋值
        }
        int update = informationService.update(information);    //执行更新
        if (update == 1) {
            return RespJson.pack(200, "更新成功", null);
        } else {
            return RespJson.pack(500, "更新失败", null);
        }
    }


    /**
     * 删除一个资讯
     *
     * @param id
     * @return
     */
    @GetMapping("/information/del/{id}")
    @ResponseBody
    //@PathVariable 路径参数绑定
    public Map<String, Object> del(@PathVariable("id") Integer id) {
        int del = informationService.del(id);   //执行删除
        if (del == 1) {
            return RespJson.pack(200, "删除成功", null);
        } else {
            return RespJson.pack(500, "删除失败", null);
        }
    }

    /**
     * 添加一个资讯类型
     */
    @PostMapping("/informationType/add")
    @ResponseBody
    public Map<String, Object> add(InformationType informationType) {
        int add = informationTypeService.add(informationType);
        if (add == 1) {
            return RespJson.pack(200, "添加成功", null);
        } else {
            return RespJson.pack(500, "添加失败", null);
        }
    }

    /**
     * 更新一个资讯类型
     */
    @PostMapping("/informationType/update")
    @ResponseBody
    public Map<String, Object> update(InformationType informationType) {
        int update = informationTypeService.update(informationType);
        if (update == 1) {
            return RespJson.pack(200, "更新成功", null);
        } else {
            return RespJson.pack(500, "更新失败", null);
        }
    }

    /**
     * 删除一个资讯类型
     */
    @GetMapping("/informationType/del/{id}")
    @ResponseBody
    public Map<String, Object> del_type(@PathVariable("id") Integer id) {
        int del = informationTypeService.del(id);
        if (del == 1) {
            return RespJson.pack(200, "删除成功", null);
        } else {
            return RespJson.pack(500, "删除失败", null);
        }
    }

    /**
     * 添加一个老师
     *
     * @param teacher
     * @param mpFile
     * @return
     * @throws IOException
     */
    @PostMapping("/teacher/add")
    @ResponseBody
    //@RequestParam 文件上传
    public Map<String, Object> add(Teacher teacher, @RequestParam(value = "pic", required = false) MultipartFile mpFile) throws IOException {
        if (mpFile != null) {   //文件不为空执行
            File fileTp = new File("/static");
            FileUtils.copyInputStreamToFile(mpFile.getInputStream(), fileTp);   //转成二进制
            teacher.setPicture(ImageBinaryUtil.getImageBinary(fileTp));    //赋值
        }
        int add = teacherService.add(teacher);  //执行添加
        if (add == 1) {
            return RespJson.pack(200, "添加成功", null);
        } else {
            return RespJson.pack(500, "添加失败", null);
        }
    }

    /**
     * 更新一个老师
     */
    @PostMapping("/teacher/update")
    @ResponseBody
    //@RequestParam 文件上传
    public Map<String, Object> updateTeacher(Teacher teacher, @RequestParam(value = "pic", required = false) MultipartFile mpFile) throws IOException {
        if (mpFile != null) {   //文件不为空执行
            File fileTp = new File("/static");
            FileUtils.copyInputStreamToFile(mpFile.getInputStream(), fileTp);   //转成二进制
            teacher.setPicture(ImageBinaryUtil.getImageBinary(fileTp));    //赋值
        }
        int update = teacherService.update(teacher);    //执行添加
        if (update == 1) {
            return RespJson.pack(200, "更新成功", null);
        } else {
            return RespJson.pack(500, "更新失败", null);
        }
    }

    /**
     * 删除一个老师
     */
    @GetMapping("/teacher/del/{id}")
    @ResponseBody
    //@PathVariable 路径参数绑定
    public Map<String, Object> delTeacher(@PathVariable("id") Integer id) {
        int del = teacherService.del(id);   //执行删除
        if (del == 1) {
            return RespJson.pack(200, "删除成功", null);
        } else {
            return RespJson.pack(500, "删除失败", null);
        }
    }


    /**
     * 添加一个管理员
     */
    @PostMapping("/user/add")
    @ResponseBody
    public Map<String, Object> addAdm(User user, @RequestParam(value = "pic", required = false) MultipartFile mpFile) throws IOException {
        if (mpFile != null) {   //文件不为空执行
            File fileTp = new File("/static");
            FileUtils.copyInputStreamToFile(mpFile.getInputStream(), fileTp);   //转成二进制
            user.setPicture(ImageBinaryUtil.getImageBinary(fileTp));         //赋值
        }
        int add = myUserService.add(user);
        if (add == 1) {
            return RespJson.pack(200, "添加成功", null);
        } else {
            return RespJson.pack(500, "添加失败", null);
        }
    }

    /**
     * 更新一个管理员
     */
    @PostMapping("/user/update")
    @ResponseBody
    public Map<String, Object> update(User user, @RequestParam(value = "pic", required = false) MultipartFile mpFile) throws IOException {
        if (mpFile != null) {   //文件不为空执行
            File fileTp = new File("/static");
            FileUtils.copyInputStreamToFile(mpFile.getInputStream(), fileTp);   //转成二进制
            user.setPicture(ImageBinaryUtil.getImageBinary(fileTp));         //赋值
        }
        int update = myUserService.update(user);
        if (update == 1) {
            return RespJson.pack(200, "更新成功", null);
        } else {
            return RespJson.pack(500, "更新失败", null);
        }
    }

    /**
     * 根据管理员ID删除
     */
    @GetMapping("/user/del/{id}")
    @ResponseBody
    public Map<String, Object> delete(@PathVariable("id") Integer id) {
        int del = myUserService.del(id);
        if (del == 1) {
            return RespJson.pack(200, "删除成功", null);
        } else {
            return RespJson.pack(500, "删除成功", null);
        }
    }

    /**
     * 添加一个证书
     */
    @PostMapping("/certificate/add")
    @ResponseBody
    public Map<String, Object> add(Certificate certificate, @RequestParam(value = "pic", required = false) MultipartFile mpFile) throws IOException {
        if (mpFile != null) {   //文件不为空执行
            File fileTp = new File("/static");
            FileUtils.copyInputStreamToFile(mpFile.getInputStream(), fileTp);   //转成二进制
            certificate.setPicture(ImageBinaryUtil.getImageBinary(fileTp));         //赋值
        }
        certificate.setDay(new Date());             //赋值当前时间
        int add = certificateService.add(certificate);
        if (add == 1) {
            return RespJson.pack(200, "添加成功", null);
        } else {
            return RespJson.pack(500, "添加失败", null);
        }
    }

    /**
     * 更新证书
     */
    @PostMapping("/certificate/update")
    @ResponseBody
    public Map<String, Object> update(Certificate certificate, @RequestParam(value = "pic", required = false) MultipartFile mpFile) throws IOException {
        if (mpFile != null) {   //文件不为空执行
            File fileTp = new File("/static");
            FileUtils.copyInputStreamToFile(mpFile.getInputStream(), fileTp);   //转成二进制
            certificate.setPicture(ImageBinaryUtil.getImageBinary(fileTp));         //赋值
        }
        int update = certificateService.update(certificate);
        if (update == 1) {
            return RespJson.pack(200, "更新成功", null);
        } else {
            return RespJson.pack(500, "更新失败", null);
        }
    }

    /**
     * 删除一个证书
     */
    @GetMapping("/certificate/del/{id}")
    @ResponseBody
    public Map<String, Object> delete2(@PathVariable("id") Integer id) {
        int del = certificateService.del(id);
        if (del == 1) {
            return RespJson.pack(200, "删除成功", null);
        } else {
            return RespJson.pack(200, "删除失败", null);
        }
    }

    /**
     * 产品IPFS添加
     */
    @PostMapping("/productIpfs/add")
    @ResponseBody
    public Map<String, Object> add(ProductIpfs productIpfs, @RequestParam(value = "pic", required = false) MultipartFile mpFile) throws IOException {
        if (mpFile != null) {   //文件不为空执行
            File fileTp = new File("/static");
            FileUtils.copyInputStreamToFile(mpFile.getInputStream(), fileTp);   //转成二进制
            productIpfs.setPicture(ImageBinaryUtil.getImageBinary(fileTp));         //赋值
        }
        int add = productIpfsService.add(productIpfs);
        if (add == 1) {
            return RespJson.pack(200, "添加成功", null);
        } else {
            return RespJson.pack(500, "添加失败", null);
        }
    }

    /**
     * 产品IPFS更新
     */
    @PostMapping("/productIpfs/update")
    @ResponseBody
    public Map<String, Object> update(ProductIpfs productIpfs, @RequestParam(value = "pic", required = false) MultipartFile mpFile) throws IOException {
        if (mpFile != null) {   //文件不为空执行
            File fileTp = new File("/static");
            FileUtils.copyInputStreamToFile(mpFile.getInputStream(), fileTp);   //转成二进制
            productIpfs.setPicture(ImageBinaryUtil.getImageBinary(fileTp));         //赋值
        }
        int update = productIpfsService.update(productIpfs);
        if (update == 1) {
            return RespJson.pack(200, "更新成功", null);
        } else {
            return RespJson.pack(500, "更新失败", null);
        }
    }

    /**
     * 产品IPFS删除
     */
    @GetMapping("/productIpfs/del/{id}")
    @ResponseBody
    public Map<String, Object> delByID(@PathVariable("id") Integer id) {
        int del = productIpfsService.del(id);
        if (del == 1) {
            return RespJson.pack(200, "删除成功", null);
        } else {
            return RespJson.pack(500, "删除失败", null);
        }
    }

    /**
     * 精英团队添加
     */
    @PostMapping("/group/add")
    @ResponseBody
    public Map<String, Object> add(Group group, @RequestParam(value = "pic", required = false) MultipartFile mpFile) throws Exception {

        if (mpFile != null) {   //文件不为空执行
            File fileTp = new File("/static");
            FileUtils.copyInputStreamToFile(mpFile.getInputStream(), fileTp);   //转成二进制
            group.setPicture(ImageBinaryUtil.getImageBinary(fileTp));         //赋值
        }

        int add = groupService.add(group);
        if (add == 1) {
            return RespJson.pack(200, "添加成功", null);
        } else {
            return RespJson.pack(500, "添加失败", null);
        }
    }

    /**
     * 精英团队更新
     */
    @PostMapping("/group/update")
    @ResponseBody
    public Map<String, Object> update(Group group, @RequestParam(value = "pic", required = false) MultipartFile mpFile) throws Exception {
        if (mpFile != null) {   //文件不为空执行
            File fileTp = new File("/static");
            FileUtils.copyInputStreamToFile(mpFile.getInputStream(), fileTp);   //转成二进制
            group.setPicture(ImageBinaryUtil.getImageBinary(fileTp));         //赋值
        }
        int update = groupService.update(group);
        if (update == 1) {
            return RespJson.pack(200, "更新成功", null);
        } else {
            return RespJson.pack(500, "更新失败", null);
        }
    }

    /**
     * 根据团队ID删除
     */
    @GetMapping("/group/del/{id}")
    @ResponseBody
    public Map<String, Object> delGroupById(@PathVariable("id") Integer id) {
        int del = groupService.del(id);
        if (del == 1) {
            return RespJson.pack(200, "删除成功", null);
        } else {
            return RespJson.pack(500, "删除失败", null);
        }
    }


    /**
     * 添加图片
     */
    @ResponseBody
    @PostMapping("/largeFile/addFile")
    public Map<String, Object> addFile(@RequestParam(value = "pic", required = false) MultipartFile file) {
        log.info("file=" + file);
        if (file != null && file.getSize() > 1) {
            try {
                Largefile largefile = new Largefile();
                //获取源文件的名称
                String fileName = file.getOriginalFilename();
                largefileService.add(largefile);
                return RespJson.pack(200, "添加成功", null);
            } catch (Exception e) {
                log.info("报错了" + e);
            }
        }
        return RespJson.pack(500, "添加失败", null);
    }

    /**
     * 更新图片
     */
    @ResponseBody
    @PostMapping("/largeFile/updateFile")
    public Map<String, Object> updateFile(Largefile largefile, @RequestParam("pic") MultipartFile file) {
        System.out.println("file=" + file);
        if (file != null && file.getSize() > 1) {
            try {
                Largefile larFile = new Largefile();
                //获取源文件的名称
                String fileName = file.getOriginalFilename();
                largefileService.updateFile(larFile);
                return RespJson.pack(200, "修改成功", larFile);
            } catch (Exception e) {
                System.out.println(e);
            }
        }
        return RespJson.pack(500, "修改失败", null);
    }

    /**
     * 删除图片
     */
    @ResponseBody
    @GetMapping("/largeFile/delById/{id}")
    public Map<String, Object> delById(@PathVariable("id") String id) {
        int i = largefileService.delByID(id);
        if (i == 1) {
            return RespJson.pack(200, "删除成功", null);
        } else {
            return RespJson.pack(500, "删除失败", null);
        }
    }

    /**
     * 添加新的发展历程
     */
    @ResponseBody
    @PostMapping("/developmentHistory/add")
    public Map<String, Object> addDeve(DevelopmentHistory developmentHistory) {
        int i = developmentHistoryService.add(developmentHistory);
        if (i == 1) {
            return RespJson.pack(200, "添加成功", i);
        } else {
            return RespJson.pack(500, "添加失败", null);
        }
    }

    /**
     * 更新发展历程
     */
    @ResponseBody
    @PostMapping("/developmentHistory/update")
    public Map<String, Object> updateDeve(DevelopmentHistory developmentHistory) {
        int i = developmentHistoryService.update(developmentHistory);
        if (i == 1) {
            return RespJson.pack(200, "更新成功", i);
        } else {
            return RespJson.pack(500, "更新成功", null);
        }
    }

    /**
     * 删除一个发展历程
     */
    @ResponseBody
    @GetMapping("/developmentHistory/del/{id}")
    public Map<String, Object> delDeve(DevelopmentHistory developmentHistory) {
        int i = developmentHistoryService.del(developmentHistory);
        if (i == 1) {
            return RespJson.pack(200, "删除成功", null);
        } else {
            return RespJson.pack(500, "删除失败", null);
        }
    }

    /**
     * 添加一条风采数据
     */
    @ResponseBody
    @PostMapping("/elegantDemeanour/add")
    public Map<String, Object> addDemeanour(ElegantDemeanour elegantDemeanour) {
        boolean b = elegantDemeanourService.save(elegantDemeanour);
        if (b == true) {
            return RespJson.pack(200, "添加成功", b);
        } else {
            return RespJson.pack(500, "添加失败", null);
        }
    }

    /**
     * 更新我们的风采
     */
    @ResponseBody
    @PostMapping("/elegantDemeanour/update")
    public Map<String, Object> updateDemeanour(ElegantDemeanour elegantDemeanour) {
        boolean b = elegantDemeanourService.updateById(elegantDemeanour);
        if (b == true) {
            return RespJson.pack(200, "更新成功", b);
        } else {
            return RespJson.pack(500, "更新失败", null);
        }
    }

    /**
     * 删除一条我们的风采数据
     */
    @ResponseBody
    @GetMapping("/elegantDemeanour/del/{id}")
    public Map<String, Object> delDemeanour(ElegantDemeanour elegantDemeanour) {
        boolean b = elegantDemeanourService.removeById(elegantDemeanour);
        if (b == true) {
            return RespJson.pack(200, "删除成功", null);
        } else {
            return RespJson.pack(500, "删除失败", null);
        }
    }

    /**
     * 添加新的热图
     */
    @ResponseBody
    @PostMapping("/heatMap/add")
    public Map<String, Object> addHeatMap(HeatMap heatMap) {
        boolean heatmap = heatMapService.save(heatMap);
        if (heatmap == true) {
            return RespJson.pack(200, "添加成功", heatmap);
        } else {
            return RespJson.pack(500, "添加失败", null);
        }
    }

    /**
     * 更新热图
     */
    @ResponseBody
    @PostMapping("/heatMap/update")
    public Map<String, Object> updateHeatMap(HeatMap heatMap) {
        boolean b = heatMapService.updateById(heatMap);
        if (b == true) {
            return RespJson.pack(200, "更新成功", b);
        } else {
            return RespJson.pack(500, "更新失败", null);
        }
    }

    /**
     * 删除一个热图
     */
    @ResponseBody
    @GetMapping("/heatMap/del/{id}")
    public Map<String, Object> delHeatMap(HeatMap heatMap) {
        boolean b = heatMapService.removeById(heatMap);
        if (b == true) {
            return RespJson.pack(200, "删除成功", null);
        } else {
            return RespJson.pack(500, "删除失败", null);
        }
    }

    /**
     * 添加新的诚聘英才数据
     */
    @ResponseBody
    @PostMapping("/recruitment/add")
    public Map<String, Object> addRecruitment(Recruitment recruitment) {
        boolean r = recruitmentService.save(recruitment);
        if (r == true) {
            return RespJson.pack(200, "添加成功", r);
        } else {
            return RespJson.pack(500, "添加失败", null);
        }
    }

    /**
     * 更新诚聘英才数据
     */
    @ResponseBody
    @PostMapping("/recruitment/update")
    public Map<String, Object> updateRecruitment(Recruitment recruitment) {
        boolean b = recruitmentService.updateById(recruitment);
        if (b == true) {
            return RespJson.pack(200, "更新成功", b);
        } else {
            return RespJson.pack(500, "更新失败", null);
        }
    }

    /**
     * 删除一条诚聘英才数据
     */
    @ResponseBody
    @GetMapping("/recruitment/del/{id}")
    public Map<String, Object> delRecruitment(Recruitment recruitment) {
        boolean r = recruitmentService.removeById(recruitment);
        if (r == true) {
            return RespJson.pack(200, "删除成功", null);
        } else {
            return RespJson.pack(500, "删除失败", null);
        }
    }

    /**
     * 添加一条公益活动数据
     */
    @ResponseBody
    @PostMapping("/publicInterest/add")
    public Map<String,Object> addPublicInterest(PublicInterest publicInterest) {
        boolean b = publicInterestService.save(publicInterest);
        if (b == true) {
            return RespJson.pack(200,"添加成功",b);
        }else {
            return RespJson.pack(500,"添加失败",null);
        }
    }

    /**
     * 更新一条公益活动数据
     */
    @ResponseBody
    @PostMapping("/publicInterest/update")
    public Map<String,Object> updPublicInterest(PublicInterest publicInterest) {
        boolean b = publicInterestService.updateById(publicInterest);
        if (b == true) {
            return RespJson.pack(200,"更新成功",b);
        }else {
            return RespJson.pack(500,"更新失败",null);
        }
    }

    /**
     * 删除一条公益活动数据
     */
    @ResponseBody
    @GetMapping("/publicInterest/del/{id}")
    public Map<String,Object> delPublicInterest(PublicInterest publicInterest) {
        boolean b = publicInterestService.removeById(publicInterest);
        if (b == true) {
            return RespJson.pack(200,"删除成功",null);
        }else {
            return RespJson.pack(500,"删除失败",null);
        }
    }
}